# -*- coding: utf-8 -*-

from apps import configs


class UserModel(object):

    def __init__(self, db):
        self.db = db
        self.table_name = '%suser' % configs.TABLE_NAME_PREFIX

    async def get_user_by_email_and_password(self, email, password):
        sql = 'SELECT * FROM %s ' % self.table_name
        sql += 'WHERE email=%s AND password=%s'
        return await self.db.fetchone(sql, (email, password))

    async def get_user_by_email(self, email):
        sql = 'SELECT * FROM %s ' % self.table_name
        sql += 'WHERE email=%s'
        return await self.db.fetchone(sql, email)

    async def get_user_by_id(self, userid):
        sql = 'SELECT * FROM %s ' % self.table_name
        sql += 'WHERE id=%s'
        return await self.db.fetchone(sql, userid)

    async def add_new_user(self, name, status, password, email):
        sql = 'INSERT INTO %s ' % self.table_name
        sql += '(name,status,password,email) VALUES(%s,%s,%s,%s)'
        return await self.db.execute(sql, (name, status, password, email))

    async def set_user_password(self, userid, password):
        sql = 'UPDATE %s ' % self.table_name
        sql += 'SET password=%s WHERE id=%s'
        return await self.db.execute(sql, (password, userid))

    async def set_user_email_status(self, userid, email_status):
        sql = 'UPDATE %s ' % self.table_name
        sql += 'SET email_status=%s WHERE id=%s'
        return await self.db.execute(sql, (email_status, userid))
